<?php
session_start();
if(isset($_SESSION['userid'])&&$_SESSION['level']!=2)
{
	
	header ("location: login.php");
	exit();
}

?>
<?php
include("includes/top_user.php");
include("includes/top-panel.php");
?>
<div id="wrapper">
	<div id="content">
		<h3>Chỉnh sửa giao diện menu</h3>
		<table>
			<thead>
				<tr>
		            <!-- <th width="100px"><a href="#">Mã Loại<img src="img/icons/arrow_down_mini.gif" width="16" height="16" align="absmiddle" /></a></th> -->
		            <th><a href="#">Tên menu</a></th>
		            <th><a href="#">Đường dẫn menu</a></th>
		            <th><a href="#">Ẩn/Hiện</a></th>
		            <th><a href="#">Thực thi</a></th>
		            
		         </tr>
		     </thead>
		     <tbody>
		         <?php
				 include("includes/connect.php");
				 $sql="SELECT * FROM tb_menu";
				 $query=mysql_query($sql);
				 
				 if(mysql_num_rows($query) == "")
				 {
					 echo "<tr><td colspan=5 align=center>Chưa có menu nào</td></tr>";
				}
				else
				{
					
					while($row=mysql_fetch_array($query))
					{
					echo "<tr>";
						echo "<td>$row[menu_name]</td>";
						echo "<td>$row[menu_link]</td>";
						if ($row["menu_status"]==1) {
							echo "<td>Đã hiện</td>";
						}else{
							echo "<td>Đã ẩn</td>";
						}
						?>
						
							<td>
								  <a data-reveal-id="myModal" id="<?php echo $row['menu_id'];?>" menu_name="<?php echo $row['menu_name']; ?>" menu_link="<?php echo $row['menu_link']; ?>" menu_status = "<?php echo $row['menu_status'];?>" href="#" onclick="update_menu(this);"><img src="img/icons/user_edit.png" title="Sửa" width="16" height="16" /></a>
								  <a id="<?php echo $row['menu_id']; ?>" href="#"  onclick="delete_menu(this);"><img src="img/icons/user_delete.png" title="Xóa" width="16" height="16" /></a>
							</td>
						
					<?php
					echo "</tr>";
					}
				}
			
				 ?>
			</tbody>
		</table>
		<br><br>
		<h3>Thêm menu</h3>
		<form id="fr_add_menu">
			<table>
			     	<tr>
			            <td>Tên menu</td>
			            <td><input type="text" id="menu_name" name="menu_name" class="menu_name" /></td>
			         </tr>
			         <tr>
			            <td>Đường dẫn</td>
			            <td><input type="text" id="menu_link" name="menu_link" class="menu_link" /></td>
			         </tr>
			         <tr>
			            <td>Ẩn/Hiện</td>
			            <td>
			            	<select id="menu_status" name="menu_status">
			            		<option value="0">Ẩn</option>
			            		<option value="1">Hiện</option>
			            	</select>
			            </td>
			         </tr>
			         <tr>
			            <td colspan="2" align="center"><input type="button" name="menu_name" class="menu_name" onclick="insertMenu();" value="Thêm vào" /></td>
			         </tr>
		    </table>
		</form>

		<!--Modal Update-->
		<div id="myModal" class="reveal-modal" data-animation="fadeAndPop"  style="width:250px">
			<form id="fr_update_menu">
				<table>
				     	<tr>
				            <td>Tên menu</td>
				            <td><input type="text" id="menu_name" name="menu_name" class="menu_name" /></td>
				         </tr>
				         <tr>
				            <td>Đường dẫn</td>
				            <td><input type="text" id="menu_link" name="menu_link" class="menu_link" /></td>
				         </tr>
				         <tr>
				            <td>Ẩn/Hiện</td>
				            <td>
				            	<select id="menu_status" name="menu_status">
				            		<option value="0">Ẩn</option>
				            		<option value="1">Hiện</option>
				            	</select>
				            </td>
				         </tr>
				         <tr>
				            <td colspan="2" align="center"><input type="button" name="menu_name" class="menu_name" onclick="insertMenu();" value="Thêm vào" /></td>
				         </tr>
			    </table>
			</form>
		</div>


	</div>
	<?php
			include("includes/sidebar.php");
	?>
</div>
<link href="../style/reveal/reveal.css" rel="stylesheet">
<script type="text/javascript">
function update_menu(element){
		var menu_name = $(element).attr("menu_name");
		var menu_link = $(element).attr("menu_link");
		// alert(menu_name);
		// alert(menu_link);
	}
function insertMenu(){
		if (checkEmpty($("#menu_name")) && checkEmpty($("#menu_link"))) {
			$.ajax({
				url : "act/insert_menu.php",
				method : "POST",
				data : $("#fr_add_menu").serialize(),
				dataType : "json",
				success : function(obj){
					if (obj==0) {
						alert("Add success !");
						location.reload();
					}
				}
			});
		}
	}
function checkEmpty(element){
	if (element.val()=="") {
		alert("Pls fill all the empty fields!");
		return false;
	}else{
		return true;
	}
}

function delete_menu(element){
	var id = element.id;
	var cfirm = confirm('Are u sure want to delete this menu?');
	if (cfirm) {
		$.ajax({
				url : "act/act_menu.php",
				method : "POST",
				data : "menu_id="+id,
				dataType : "json",
				success : function(obj){
					if (obj==0) {
						alert("Delete done!");
						location.reload();
					}
				}
			});
	};
}

</script>
<script src="../style/reveal/jquery.reveal.js" type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<?php
include("includes/footer.php");
?>